﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using OpenExpressApp.Editors;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows;
using OpenExpressApp.MetaModel.Utils;

namespace OpenExpressApp.Module.WPF.Editors
{
    /// <summary>
    /// GridColumn基类
    /// </summary>
    public abstract class OpenDataGridColumn : DataGridColumn
    {
        private BusinessObjectPropertyInfo _propertyInfo;

        /// <summary>
        /// 所属的视图
        /// </summary>
        private IListObjectView _view;

        public OpenDataGridColumn(BusinessObjectPropertyInfo info, IListObjectView view)
            : base()
        {
            if (info == null) throw new ArgumentNullException("info");
            if (view == null) throw new ArgumentNullException("view");

            this._view = view;
            this.PropertyInfo = info;
        }

        /// <summary>
        /// 默认没有Editor
        /// </summary>
        public virtual IWPFPropertyEditor Editor
        {
            get
            {
                return null;
            }
        }

        /// <summary>
        /// 正在编辑的属性
        /// </summary>
        public BusinessObjectPropertyInfo PropertyInfo
        {
            get
            {
                return this._propertyInfo;
            }
            set
            {
                this._propertyInfo = value;
                var editor = this.Editor;
                if (editor != null)
                {
                    editor.PropertyInfo = value;
                }
            }
        }

        /// <summary>
        /// 所属的视图
        /// </summary>
        public IListObjectView View
        {
            get
            {
                return this._view;
            }
        }

        /// <summary>
        /// 默认以TextBlock显示
        /// </summary>
        /// <param name="cell"></param>
        /// <param name="dataItem"></param>
        /// <returns></returns>
        protected override FrameworkElement GenerateElement(DataGridCell cell, object dataItem)
        {
            TextBlock textBlock = new TextBlock();

            Binding textBinding = new Binding(this._propertyInfo.Name)
            {
                StringFormat = this._propertyInfo.StringFormat
            };
            textBlock.SetBinding(TextBlock.TextProperty, textBinding);

            var propertyType = this._propertyInfo.RuntimeProperty.PropertyType;
            if (TypeHelper.IsNumber(propertyType))
            {
                textBlock.TextAlignment = TextAlignment.Right;
            }

            return textBlock;
        }

        protected override FrameworkElement GenerateEditingElement(DataGridCell cell, object dataItem)
        {
            var editor = this.Editor;
            //如果没有编辑器，则不能生成编辑控件
            if (editor == null) throw new NotSupportedException("只读！");

            editor.EnsureControlsCreated();
            return editor.IsReadOnly ? editor.ReadOnlyControl : editor.Control;
        }
    }
}